﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Globalization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;

namespace Simhopp.Common
{
    /// <summary>
    /// The Competition class provides a description of a diving competition.
    /// </summary>
  
  public class Competition
    {
        

        #region CONSTRUCTOR REGION



        public Competition()
        {
            DivingTypeList = new List<DivingType>();
        }

        #endregion

        #region PROPERTY REGION

        //Creates three rounds


        public List<DivingType> DivingTypeList { get; set; }

        #endregion

        #region METHOD REGION

        public void LoadDivingTypesFromFile()
        {

            //This is needed in order to make sure that the float values can be inputted into the SQL database.
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");


            //Only use this function to input all the diffrent types of jumps into the databse!
            StreamReader sr = new StreamReader("hopp2.txt");

            string line;

            while (!sr.EndOfStream)
            {
                DivingType Temp = new DivingType();

                line = sr.ReadLine();

                string[] sp = line.Split('\t');

                sp[1] = sp[1].Remove(sp[1].Length - 1);

                int TempIntHeight = Convert.ToInt16(sp[1]);
                double TempDoublePointsMultiplier = Convert.ToDouble(sp[3], System.Globalization.CultureInfo.InvariantCulture);



                //Seach through the entire list d for previous occurances of the sp[0] ID.
                //If a previous one was found, just add the newly found height and constant to its list.
                Boolean HeightConstantAdded = false;
                foreach (var TempTemp in DivingTypeList)
                {
                    if (TempTemp.ID == sp[0])
                    {
                        
                        TempTemp.AddHeight((Heights)TempIntHeight);
                        TempTemp.AddMultiplier(TempDoublePointsMultiplier);

                        HeightConstantAdded = true;
                        break;
                    }
                }


                //This if handles the situation where a entierly new object must be added to the list.
                if (!HeightConstantAdded)
                {
                    Temp.ID = sp[0];
                    Temp.AddHeight((Heights)TempIntHeight);
                    Temp.TypeName = sp[2];
                    Temp.AddMultiplier(TempDoublePointsMultiplier);
                    DivingTypeList.Add(Temp);
                }
            }


            foreach (var Pryl in DivingTypeList)
            {
                foreach (var shit in Pryl.Height)
                    Console.Write(" " + shit);

                Console.Write(" " + Pryl.TypeName + '\t');
                foreach (var shit in Pryl.DifficultyFactor)
                    Console.Write(" " + shit);

                Console.Write('\n');
            }


        }


        public void ReadAllInfoFromFileIntoDataBase()
        {
            //T002E004484\KBMSS
            SqlConnection s = new SqlConnection();

            SqlConnection myConnection = new SqlConnection(
                                       @"Data Source=T002E004478\KBMSS;" +
                                       "Trusted_Connection= True;" +
                                       "Integrated Security=True;" +
                                       @"Initial Catalog=TypeOfJump;" +
                                       "connection timeout=5;" +
                                        "MultipleActiveResultSets = True;");

            try
            {
                myConnection.Open();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }



            //Pul all the good shit in the table!

            Competition c = new Competition();

            c.LoadDivingTypesFromFile();


            for (int i = 0; i < c.DivingTypeList.Count; i++)
            {
                for (int j = 0; j < c.DivingTypeList[i].Height.Count; j++)
                {
                    Console.WriteLine("INSERT INTO Table_1(ID, TypeOfJump, Height, Difficulty) VALUES('" + c.DivingTypeList[i].ID + "'" + "," + "'" + c.DivingTypeList[i].TypeName + "'," + c.DivingTypeList[i].Height[j] + "," + (float)c.DivingTypeList[i].DifficultyFactor[j] + ")");
                    SqlCommand MyCommand = new SqlCommand("INSERT INTO Table_1(ID, TypeOfJump, Height, Difficulty) VALUES('" + c.DivingTypeList[i].ID + "'" + "," + "'" + c.DivingTypeList[i].TypeName + "'," + c.DivingTypeList[i].Height[j] + "," + (float)c.DivingTypeList[i].DifficultyFactor[j] + ")", myConnection);
                    MyCommand.ExecuteNonQuery();
                }

            }


            Console.WriteLine("ALLT INLÄST FRÅN FILEN");
           

        }

        public void TestReadFromSQL()
        {
            SqlConnection s = new SqlConnection();

            SqlConnection myConnection = new SqlConnection(
                                       @"Data Source=T002EOO4478\KBMSS;" +
                                       "Trusted_Connection= True;" +
                                       "Integrated Security=True;" +
                                       @"Initial Catalog=TypeOfJump;" +
                                       "connection timeout=5;" +
                                        "MultipleActiveResultSets = True;");

            myConnection.Open();

            SqlDataReader MyReader = null;

            SqlCommand MyCommand = new SqlCommand("Select * from JumpName", myConnection);


            MyReader = MyCommand.ExecuteReader();
            while (MyReader.Read())
            {
                Console.WriteLine(MyReader[0].ToString());
                Console.WriteLine(MyReader[1].ToString());
                //Console.WriteLine(MyReader[2].ToString());
                //Console.WriteLine(MyReader[3].ToString());

                Console.WriteLine();
            }
        }

        public void ReadAllInfoFromSQL()
        {
            SqlConnection s = new SqlConnection();

  
            SqlConnection myConnection = new SqlConnection(
                                       @"Data Source=T002EOO4478\KBMSS;" +
                                       "Trusted_Connection= True;" +
                                       "Integrated Security=True;" +
                                       @"Initial Catalog=TypeOfJump;" +
                                       "connection timeout=5;" +
                                        "MultipleActiveResultSets = True;");
            try
            {
                myConnection.Open();
            }
            catch
            {
                Console.WriteLine("Error connecting to SQL server");
                return;
            }


            //Fill up the DivingType class with all the possible jumps.





            myConnection.Close();
        }

        #endregion
    }
}
